namespace ui {

    export function createImg(src?: string | egret.Texture,
        s9grids?: hjc.S9GridsOpitons): eui.Image {
        let image = new eui.Image(src);
        if (notEmpty(s9grids)) ui.setS9grids(image, s9grids);
        return image;
    }

    export function setS9grids(image: eui.Image, s9grids: hjc.S9GridsOpitons | null): eui.Image {
        if (s9grids === null) image.scale9Grid = null;
        else if(notEmpty(s9grids)) image.scale9Grid = new egret.Rectangle(s9grids.x, s9grids.y, s9grids.w, s9grids.h);
        return image;
    }


    /**
     * 创建文本组件
     * @param text 文本内容
     * @param params 文本显示参数 *默认无
     * @return 创建的文本
     */
    export function newLbl(text?: string | hjc.StyleText, params?: hjc.TText): eui.Label {
        let lbl = new eui.Label();
        if (isString(text)) lbl.text = text as string;
        else if (text instanceof hjc.StyleText) lbl.textFlow = text.getElems();
        lbl.touchEnabled = false;//无交互      
        return setText(lbl, params) as eui.Label;
    }

    /**
     * 创建可编辑文本组件
     * @param promp 提示文字
     * @param params 文本显示参数 *默认无
     * @return 创建的可编辑文本
     */
    export function newEdtText(prompt?: string, params?: hjc.TText): eui.EditableText {
        let edTxt = new eui.EditableText();
        if (isString(prompt)) edTxt.prompt = prompt;
        edTxt.maxChars = (params && numeric(params.maxLen)) ? params.maxLen : Font.Default.MAX_LEN;//最大长度
        edTxt.touchEnabled = true;
        return setText(edTxt, params) as eui.EditableText;
    }

    /**
    * 重置文本样式
    * @param obj 文本组件
    * @param params 文本显示参数
    * @return 文本组件自身
    */
    export function setText<T extends egret.TextField>(obj: T, params: hjc.TText): T {
        obj.inputType = egret.TextFieldInputType.TEXT;//文本类型
        if (isEmpty(params)) return obj;
        obj.textColor = numeric(params.color) ? params.color : Color.Default.TEXT;//文本颜色
        obj.size = numeric(params.size) ? params.size : Font.Size.MID;//字体大小
        if (isBoolean(params.bold)) obj.bold = params.bold;//粗体
        if (isBoolean(params.italic)) obj.italic = params.italic;//斜体
        if (isBoolean(params.multiline)) obj.multiline = params.multiline;//多行文本
        if (numeric(params.stroke)) obj.stroke = params.stroke;//描边
        if (numeric(params.strokeColor)) obj.strokeColor = params.strokeColor;//描边颜色
        if (numeric(params.backgroundColor)) { obj.background = true; obj.backgroundColor = params.backgroundColor; }//背景颜色
        if (numeric(params.borderColor)) { obj.border = true; obj.borderColor = params.borderColor; }//边框颜色
        if (isBoolean(params.password)) obj.displayAsPassword = params.password;//密码显示
        obj.fontFamily = isString(params.family) ? params.family : Font.Default.FAMILY;//字体
        if (numeric(params.lineSpacing)) obj.lineSpacing = params.lineSpacing;//行间距
        obj.textAlign = isString(params.horAlign) ? params.horAlign : egret.HorizontalAlign.LEFT;//水平居左
        obj.verticalAlign = isString(params.verAlign) ? params.verAlign : egret.VerticalAlign.MIDDLE; //垂直居中
        if (isBoolean(params.wrap)) obj.wordWrap = params.wrap;//整词换行
        return obj;
    }

    /**
     * 创建矩形绘图
     * @param params 矩形绘图显示参数（参见hjc.TRect）
     * @return 创建的矩形绘图
     */
    export function newRect(params: hjc.TRect): eui.Rect {
        let rect = new eui.Rect(hjc.UI.Default.WIDTH, hjc.UI.Default.HEIGHT, Color.Default.FILL);
        if (notEmpty(params)) {
            if (isString(params.name)) rect.name = params.name as string;
            if (numeric(params.size)) { rect.width = params.size; rect.height = params.size; }
            if (numeric(params.width)) rect.width = params.width;
            if (numeric(params.height)) rect.height = params.height;
            if (numeric(params.color)) rect.fillColor = params.color;
            if (numeric(params.alpha)) rect.fillAlpha = params.alpha;
            if (numeric(params.stroke)) rect.strokeWeight = params.stroke;
            if (numeric(params.strokeColor)) rect.strokeColor = params.strokeColor;
            if (numeric(params.strokeAlpha)) rect.strokeAlpha = params.strokeAlpha;
            if (isBoolean(params.touchEnabled)) rect.touchEnabled = params.touchEnabled;
        }
        return rect;
    }

    /**
     * 设置组件对齐方式(四周边距和居中偏移)
     * @param comp 组件
     * @param options 对齐参数
     * @param parent 需要加入的父组件
     * @return 组件自身
     */
    export function setAlign<T extends eui.UIComponent>(comp: T,
        options: hjc.AlignOptions, parent?: eui.Component | eui.Group): T {
        if (parent) parent.addChild(comp);
        if (numeric(options.all)) { comp.left = options.all; comp.right = options.all; comp.top = options.all; comp.bottom = options.all; }
        if (numeric(options.ver)) { comp.top = options.ver; comp.bottom = options.ver; }
        if (numeric(options.hor)) { comp.left = options.hor; comp.right = options.hor; }
        if (numeric(options.left)) comp.left = options.left;
        if (numeric(options.right)) comp.right = options.right;
        if (numeric(options.top)) comp.top = options.top;
        if (numeric(options.bottom)) comp.bottom = options.bottom;
        if (!!options.centered) { comp.horizontalCenter = 0; comp.verticalCenter = 0; }
        if (numeric(options.vc)) comp.verticalCenter = options.vc;
        if (numeric(options.hc)) comp.horizontalCenter = options.hc;
        return comp;
    }

    /**
   * 移除组件四周边距
   * @param obj 控件
   * @return 子控件自身
   */
    export function clearPadding<T extends eui.UIComponent>(obj: T, ): T {
        obj.left = undefined; obj.right = undefined; obj.top = undefined; obj.bottom = undefined;
        return obj;
    }

    /**
     * 设置组件填充满父控件
     * @param child 子控件
     * @param parent 需要加入的父控件 *默认无
     * @return 子控件自身
     */
    export function setFull<T extends eui.UIComponent>(child: T, parent?: eui.Component | eui.Group): T {
        return setAlign(child, { all: 0 }, parent);
    }

    /**
     * 设置组件居中
     * @param comp 组件
     * @param parent 需要加入的父组件
     * @param options 居中参数
     * @return 组件自身
     */
    export function setCenter<T extends eui.UIComponent>(comp: T, parent?: egret.DisplayObjectContainer, option?: hjc.AlignOptions): T {
        comp.verticalCenter = 0; comp.horizontalCenter = 0;
        if (parent) parent.addChild(comp);
        if (!!option) {
            if (numeric(option.vc)) comp.verticalCenter = option.vc;
            if (numeric(option.hc)) comp.horizontalCenter = option.hc;
        }
        return comp;
    }

    /**
    * 为列表重新填充项目视图
    * @param container 显示容器组
    * @param itemClass 视图类型
    * @param dataArr 数据源数组
    * @return 显示容器组自身
    */
    export function reloadItems<I extends hjc.Item>(container: eui.Group, itemClass: new (...params: any[]) => I, dataArr: any[]): eui.Group {
        let items = ui.getChildren<hjc.Item>(container);
        if (items.length >= dataArr.length) items.forEach((item: any, i: number) => dataArr.length > i ? item.update(dataArr[i]) : ui.remove(item));
        else dataArr.forEach((data: any, i: number) => items.length <= i ? container.addChild(new itemClass(data)) : items[i].update(dataArr[i]));
        return container;
    }

    /**
     * 创建富文本
     * @param text 文本内容
     * @param style 文本风格
     */
    export function newStyle(text: string, style?: egret.ITextStyle): hjc.StyleText {
        return new hjc.StyleText().add(text, style);
    }
}